// 模拟机试输入情况
// 第一行输入：接下来要输入的总行数
// 第二~n行输入：每一行都代表需要 a + b的值
const zddTool = require('../../commonTool/zddtool.js');
const readline = require('readline');

const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout,
});

let inputTimes = 0;
let inputMaxTimes = 10; // 最多读取多少行，默认10行

console.log('\nPlease input the number of inputs:');

rl.on('line', (line) => {
    try {
        if (inputTimes == 0) {
            if (!zddTool.isPositiveNumber(line)) {
                throw new Error('ERROR! The input is not legal');
            } else {
                inputMaxTimes = parseInt(line) + 1;
            }
        } else {
            if (!zddTool.isNumberArray(line)) {
                throw new Error('ERROR! The input is not legal');
            }
            var lines = line.split(' ');
            var a = parseInt(lines[0]);
            var b = parseInt(lines[1]);
            console.log(`result: ${a}+${b} => ${a + b}`);
        }
    } catch (ex) {
        console.error(ex.message);
        rl.close();
    } finally {
        inputTimes++;
        if (inputTimes >= inputMaxTimes) {
            rl.close();
        } else {
            console.log(
                `Wait for next input[${inputTimes - 1}/${inputMaxTimes - 1}]...`
            );
        }
    }
});

rl.on('close', () => {
    console.log('\nReadline interface closed.');
    process.exit(0);
});
